package com.anivia.system.auth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.hutool.core.lang.tree.Tree;
import com.anivia.common.core.constant.PlatformTypeConstant;
import com.anivia.common.core.model.vo.R;
import com.anivia.common.web.satoken.util.UserInfoUtil;
import com.anivia.system.auth.model.vo.ResourceInfoVO;
import com.anivia.system.auth.service.IRoleResourceInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author wangj
 */
@Tag(name = "资源管理")
@RestController
@RequestMapping("/common/resource")
public class ResourceInfoCommonController {
    @Resource
    private IRoleResourceInfoService resourceInfoService;

    @Operation(summary = "查询当前用户有权限的资源(树形结构)")
    @PostMapping("resources")
    @SaCheckOr(
            login = {@SaCheckLogin(type = PlatformTypeConstant.USER)
                    , @SaCheckLogin(type = PlatformTypeConstant.TENANT)
                    , @SaCheckLogin(type = PlatformTypeConstant.ADMIN)}
    )
    private R<List<ResourceInfoVO>> resources() {
        return R.ok(resourceInfoService.resources());
    }

    @Operation(summary = "查询所有资源(树形结构)")
    @PostMapping("baseTree")
    @SaCheckOr(
            login = {@SaCheckLogin(type = PlatformTypeConstant.USER)
                    , @SaCheckLogin(type = PlatformTypeConstant.TENANT)
                    , @SaCheckLogin(type = PlatformTypeConstant.ADMIN)}
    )
    private R<List<Tree<Integer>>> baseTree() {
        return R.ok(resourceInfoService.baseTree(UserInfoUtil.getLoginType()));
    }
}
